package com.chuangke.mds.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chuangke.common.db.page.CkPageQuery;
import com.chuangke.common.idgen.IdManager;
import com.chuangke.common.trans.annotation.TransResult;
import com.chuangke.mds.dao.TechnologyTypeParamConfDao;
import com.chuangke.mds.entity.TechnologyTypeParamConf;
import com.chuangke.mds.service.TechnologyTypeParamConfService;

import cn.hutool.core.util.StrUtil;

@Service
@Transactional(rollbackFor = Throwable.class)
public class TechnologyTypeParamConfmpl implements TechnologyTypeParamConfService {
	
	@Autowired
	private TechnologyTypeParamConfDao paramConfDao;
	
	@Override
	public String save(TechnologyTypeParamConf model) {
		return saveOrUpdate(model);
	}
	
	@Override
	public void update(TechnologyTypeParamConf model) {
		saveOrUpdate(model);
	}

	@Override
	public String saveOrUpdate(TechnologyTypeParamConf model) {
		if (StrUtil.isBlank(model.getId())) {
			doSave(model) ;
		}else{
			doUpdate(model) ;
		}
		return model.getId() ;
	}
	
	private void doSave(TechnologyTypeParamConf model) {
		model.setId(IdManager.nextId());
		paramConfDao.save(model) ;
	}
	
	private void doUpdate(TechnologyTypeParamConf model) {
		paramConfDao.updateById(model) ;
	}

	@Override
	public void delete(List<String> billIds) {
//		QueryWrapper<TechnologyTypeParamConf> qw = new QueryWrapper<TechnologyTypeParamConf>() ;
//		qw.lambda().in(TechnologyTypeParamConf::getId, billIds) ;
//		List<ZchkclTechnologyTypeParamConf> list = taskDelayDao.list(qw) ;
//		for(ZchkclTechnologyTypeParamConf delay : list) {
//			if(!FlowConstant.STATUS_BZZ.equals(delay.getStatus())) {
//				throw new ChuangkeException("只能删除编制中的业务单据") ;
//			}
//		}
		
        paramConfDao.remove(new QueryWrapper<TechnologyTypeParamConf>().lambda().in(TechnologyTypeParamConf::getId, billIds));
	}

	@Override
	@TransResult
	public TechnologyTypeParamConf load(String billId) {
		TechnologyTypeParamConf paramConf =paramConfDao.getById(billId);
		return paramConf;
	}
	@Override
	@TransResult
	public List<TechnologyTypeParamConf> getList(String confType) {
		List<TechnologyTypeParamConf> paramConfList =paramConfDao.list(new QueryWrapper<TechnologyTypeParamConf>().lambda().eq(TechnologyTypeParamConf::getConfType, confType));
		return paramConfList;
	}

	@Override
	@TransResult
	public IPage<TechnologyTypeParamConf> page(CkPageQuery page) {
		QueryWrapper<TechnologyTypeParamConf> qw = new QueryWrapper<>();
		if(!StringUtils.isEmpty(page.getAsString("type"))) {
			qw.lambda().eq(TechnologyTypeParamConf::getType, page.getAsString("type")) ;
		}
		
		qw.lambda().orderByDesc(TechnologyTypeParamConf::getType).orderByDesc(TechnologyTypeParamConf::getCreatedAt);
		return paramConfDao.page(page.getPage(),qw);
	}

}
